package com.ruoyi.web.utils.influxdb;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.enums.AggreEnum;
import com.ruoyi.web.enums.FactorEnum;
import lombok.Data;

import java.time.temporal.ChronoUnit;
import java.util.*;


/**
 * @desc  聚合查询参数
 * @author fwx 2024/2/23 16:57
 **/
@Data
public class DeviceAggreDataParam {

    private String deviceId;

    private Long productId;

    private String startTime;

    private String endTime;

    //查询的因子
    private String factor;

    //因子管理中的key
    private String factorKey;

    //查询类型 max min sum count mean
    private AggreEnum type;

    //数据开窗维度
    private ChronoUnit chronoUnit = ChronoUnit.DAYS;

    private static Set<String> FILTER_FACTOR_SET = new HashSet<>();

    static {
        FILTER_FACTOR_SET.add(FactorEnum.WH_RAIN.getKey());
        FILTER_FACTOR_SET.add(FactorEnum.WH_WIND_SPEED.getKey());
    }

    public long getStartTimeSecond() {
        return DateUtil.parse(startTime, DatePattern.NORM_DATE_PATTERN).getTime() / 1000;
    }

    public long getEndTimeSecond() {
        return DateUtil.parse(endTime, DatePattern.NORM_DATE_PATTERN).getTime() / 1000 + 86400; //一天86400秒
    }

//    /**
//     * @desc  构建InfluxDB查询对象
//     * @author fwx 2024/3/1 16:12
//     **/
//    public Restrictions buildRestrictions(){
//
//        Restrictions rain;
//
//        //判断因子的值是否可以为0
//        if(FILTER_FACTOR_SET.contains(factorKey)){
//            rain = Restrictions.tag(DeviceParamUtil.FIELD_VALUE).notEqual(-1000);
//        }else {
//            rain = Restrictions.tag(DeviceParamUtil.FIELD_VALUE).notEqual(0);
//        }
//
//        Restrictions restrictions = Restrictions.and(
//                Restrictions.tag(DeviceParamUtil.FIELD_DEVICE_ID).equal(this.getDeviceId()),
//                Restrictions.tag(DeviceParamUtil.DB_DEFAULT_PROPERTY).equal(this.getFactor()),
//                rain,
//                Restrictions.tag(DeviceParamUtil.DB_DEFAULT_FIELD).equal(DeviceParamUtil.FIELD_NUMBER_VALUE),
//                Restrictions.measurement().equal(DeviceParamUtil.createPropertyMetric(this.getProductId())))
//                ;
//        return restrictions;
//    }

    public DeviceAggreDataParam() {

    }

    public DeviceAggreDataParam(String deviceId, Long productId, String factor, String factorKey) {
        this.deviceId = deviceId;
        this.productId = productId;
        this.factor = factor;
        this.factorKey = factorKey;
    }

    /**
     * @desc  得到本年的查询日期
     * @author fwx 2024/3/11 17:09
     **/
    public DeviceAggreDataParam buildYear(){
        this.setStartTime(DateUtils.getYearBeginDate());
        this.setEndTime(DateUtils.getTomorrowStr());
        return this;
    }

    /**
     * @desc  得到所有数据的时间
     * @author fwx 2024/3/11 17:21
     **/
    public DeviceAggreDataParam buildAll(){
        this.setStartTime(DateUtils.getYearBeginDate(-3)); // 3年前时间
        this.setEndTime(DateUtils.getTomorrowStr());
        return this;
    }

    /**
     * @desc  得到去年的查询日期
     * @author fwx 2024/3/11 17:23
     **/
    public DeviceAggreDataParam buildLastYear(){
        this.setStartTime(DateUtils.getYearBeginDate(-1)); // 去年开始时间
        this.setEndTime(DateUtils.getYearBeginDate());
        return this;
    }

    /**
     * @desc  得到本月的查询日期
     * @author fwx 2024/2/29 10:01
     **/
    public DeviceAggreDataParam getMonth(){
        this.setStartTime(DateUtils.getMonthBeginDate());
        this.setEndTime(DateUtils.getTomorrowStr());
        return this;
    }

    /**
     * @desc  得到去年同期的月份
     * @author fwx 2024/2/29 10:01
     **/
    public DeviceAggreDataParam getLastMonth(){
        this.setStartTime(DateUtils.getMonthBeginDate(-1,0,0));
        this.setEndTime(DateUtils.getMonthBeginDate(-1,1,-1));
        return this;
    }


    @Override
    protected DeviceAggreDataParam clone() {
        DeviceAggreDataParam request = new DeviceAggreDataParam();
        request.deviceId = this.deviceId;
        request.productId = this.productId;
        request.factor = this.factor;
        request.factorKey = this.factorKey;

        return request;
    }

    /**
     * @desc  得到往年的月份查询日期
     * @author fwx 2024/3/2 13:51
     **/
    public List<DeviceAggreDataParam> getBeforeMonth(){

        List<DeviceAggreDataParam> result = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            DeviceAggreDataParam request = this.clone();
            request.setStartTime(DateUtils.getMonthBeginDate(-1 - i,0,0));
            request.setEndTime(DateUtils.getMonthBeginDate(-1 - i,1,-1));
            result.add(request);
        }

        return result;
    }

    /**
     * @desc  得到往年的 年日期
     * @author fwx 2024/3/2 14:47
     **/
    public List<DeviceAggreDataParam> getBeforeYear(){

        List<DeviceAggreDataParam> result = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            DeviceAggreDataParam request = this.clone();
            request.setStartTime(DateUtils.getYearBeginDate(-1 - i)); // 去年开始时间
            request.setEndTime(DateUtils.getYearEndDate(0 - i));
            result.add(request);
        }

        return result;
    }

    /**
     * @desc  得到往年的 当日日期
     * @author fwx 2024/3/2 14:47
     **/
    public List<DeviceAggreDataParam> getBeforeDay(){

        List<DeviceAggreDataParam> result = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            DeviceAggreDataParam request = this.clone();
            request.setStartTime(DateUtils.getDayBeginDate(-1 - i,0));
            request.setEndTime(DateUtils.getDayBeginDate(-1 - i,0));
            result.add(request);
        }

        return result;
    }

    /**
     * @desc  得到今天的查询日期
     * @author fwx 2024/2/19 14:27
     **/
    public DeviceAggreDataParam getDay(){
        this.setStartTime(DateUtils.getDate());
        this.setEndTime(DateUtils.getTomorrowStr());
        return this;
    }


    /**
     * @desc  得到一个小时前的时间
     * @author fwx 2024/3/2 16:28
     **/
    public DeviceAggreDataParam getHour(){
        this.setStartTime(DateUtils.getDayBeginDate(-1,0));
        this.setEndTime(DateUtils.getTomorrowStr());
        return this;
    }

    /**
     * @desc  得到去年同日时间
     * @author fwx 2024/2/29 10:12
     **/
    public DeviceAggreDataParam getLastDay(){
        this.setStartTime(DateUtils.getDayBeginDate(-1,0));
        this.setEndTime(DateUtils.getDayBeginDate(-1,0));
        return this;
    }


    public static void main(String[] args) {



    }

}
